package br.com.aeon.view.flex._componentes.popupselector.controls
{

import br.com.aeon.view.flex._componentes.popupselector.events.PopUpSelectorEvent;

import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Point;

import mx.controls.Button;
import mx.core.Application;
import mx.core.Container;
import mx.core.IDataRenderer;
import mx.core.IFlexDisplayObject;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;
import mx.states.SetStyle;

[Event(name="popUpClose", type="br.com.aeon.view.flex._componentes.popupselector.events.PopUpSelectorEvent")]

public class PopUpSelector extends Button
{
	
	[Embed("assets/wizard.png")]
	private var buttonIcon:Class;
	
    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

	public function PopUpSelector()
	{
		super();
		
		moveAway = new MoveAwayFX();
		moveAway.addEventListener(MoveAwayFX.MOVE_OUT_END, moveOutEndHandler);
		moveAway.duration = 300;
		
		setStyle("icon", buttonIcon);
		setStyle("cornerRadius", 0);
		
		toggle = true;
	}
	
	
	
	//--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------
    
    /**
     *  @private
     *  Instancia da classe MoveAway responsável pelo efeito da PopUp
     */
    private var moveAway:MoveAwayFX;
	
	
	/**
     *  @private
     *  Armazena uma referência para a instância deste PopUpSelector
     *  É usado na função estática removePopUp
     */
	private static var relatedButton:PopUpSelector;
	
	
	/**
     *  @private
     *  Referência para o popUp
     */
	private var popUp:IFlexDisplayObject;
    
    
    
    //--------------------------------------------------------------------------
    //
    //  Overridden event handlers
    //
    //--------------------------------------------------------------------------
	
	override protected function clickHandler(event:MouseEvent):void
	{
		super.clickHandler(event);
		if (selected)
		{
			createpopUp();
		}
	}



	//--------------------------------------------------------------------------
    //
    // Static Methods
    //
    //--------------------------------------------------------------------------

	public static function removePopUp():void 
	{
		with (relatedButton)
		{
			if (popUp)
			{
				moveAway.affectedTarget = UIComponent(relatedButton.popUp);
				moveAway.destinationTarget = relatedButton;
				moveAway.moveOut();
			}
		}
	}



	//--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------
    
	public function createpopUp():void
	{
		popUp = PopUpManager.createPopUp(DisplayObject(parentApplication), popUpClass);
		popUp.addEventListener(FlexEvent.CREATION_COMPLETE, popUpCreationCompleteHandler);
		
		Application.application.addEventListener(MouseEvent.MOUSE_DOWN, appMouseDownHandler);
		
		relatedButton = this;
		popUp.visible = false;
	}
	
	
	
    //--------------------------------------------------------------------------
    //
    //  Event handlers
    //
    //--------------------------------------------------------------------------
	
	private function appMouseDownHandler(event:MouseEvent):void
	{
		PopUpSelector.removePopUp();
	}
	
	private function popUpCreationCompleteHandler(event:FlexEvent):void
	{
		popUp.removeEventListener(FlexEvent.CREATION_COMPLETE, popUpCreationCompleteHandler);
		
		var point:Point = Container(parent).contentToGlobal(new Point(x, y));
		popUp.move(point.x+width+xOffSet, point.y+yOffSet);
		
		moveAway.affectedTarget = UIComponent(popUp);
		moveAway.destinationTarget = this;
		moveAway.moveIn();
	}
	
	private function moveOutEndHandler(event:Event):void
	{
		
		Application.application.removeEventListener(MouseEvent.MOUSE_DOWN, appMouseDownHandler);
		
		try 
		{
			var itemChooserButtonEvent:PopUpSelectorEvent = new PopUpSelectorEvent(PopUpSelectorEvent.POP_UP_CLOSE);
			itemChooserButtonEvent.selectedItem = IDataRenderer(popUp).data;
		} 
		catch (e:Error)
		{
			throw new Error("'popUpClass' deve ser uma classe que implementa a interface IDataRenderer");
		}
		
		dispatchEvent(itemChooserButtonEvent);
		
		PopUpManager.removePopUp(popUp);
		
		popUp = null;
		
		selected = false;
		
		
	}
	
	
	
	
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------
	
	//----------------------------------
    //  popUpClass
    //----------------------------------
	
	private var _popUpClass:Class;
	
	[Bindable]
	
	public function get popUpClass():Class
	{
		return _popUpClass;
	}
	
	public function set popUpClass(value:Class):void
	{
		_popUpClass = value;
	}

	
	
	//----------------------------------
    //  yOffSet
    //----------------------------------

 	private var _yOffSet:int;
	
	[Bindable]
	
	public function get yOffSet():int
	{
		return _yOffSet;
	}
	
	public function set yOffSet(value:int):void
	{
		_yOffSet = value;
	}	

 
 
 	//----------------------------------
    //  xOffSet
    //----------------------------------
	
 	private var _xOffSet:int;
	
	[Bindable]
	
	public function get xOffSet():int
	{
		return _xOffSet;
	}
	
	public function set xOffSet(value:int):void
	{
		_xOffSet = value;
	}	
		
}

}